//
//  SINARegisterSetPasswordViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/25.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINARegisterSetPasswordViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINAUiviewClickLiserner.h"
#import "SINAHttpRequestUtils.h"
#import "SINALoginViewController.h"
#import "SINANavLoginViewController.h"
#import "SINAControllerUtils.h"
#import "SINAIdentitySelectViewController.h"

@interface SINARegisterSetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputPassword1Label;

@property (weak, nonatomic) IBOutlet UITextField *inputPassword2Label;
//提交
@property (weak, nonatomic) IBOutlet UIButton *commitLabel;


//测试 不需要验证
@property(nonatomic,assign) BOOL isTest;
@end

@implementation SINARegisterSetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super initBackItem];
    if (self.pageType==0) {
        self.navigationItem.title =@"注册";
    }else if(self.pageType==1){
        self.navigationItem.title =@"找回密码";
    }
    
    if (UI_IS_IPHONE6PLUS) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_plus_bg"]  forBarMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_bg"]  forBarMetrics:UIBarMetricsDefault];
    }
    
    
    self.isTest = NO;
    self.commitLabel.backgroundColor=baseButtonBackGroundColor;
    
    [SINACircularBeadUtils makeCircularBead:2 andView:self.commitLabel andIsSetBackground:YES];
    WeakSelf;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.commitLabel andWithBlick:^(UIView *view) {
        [weakSelf commitDataFunction];
    }];
    
    self.inputPassword1Label.secureTextEntry = YES;
    self.inputPassword2Label.secureTextEntry = YES;
}

-(void) commitDataFunction{
    
    if (self.isTest) {
        SINAIdentitySelectViewController *selectControll = [[SINAIdentitySelectViewController alloc]initWithNibName:@"SINAIdentitySelectViewController" bundle:nil];
        [self.navigationController pushViewController:selectControll animated:YES];
        return;
    }
    NSString *password1=self.inputPassword1Label.text;
    NSString *password2=self.inputPassword2Label.text;
    
    if (password1==nil||[password1 length]<1) {
        WISAlertView *alertView =
        [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入密码" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    if (password2==nil||[password2 length]<1) {
        WISAlertView *alertView =
        [[WISAlertView alloc]initWithTitle:@"提示" message:@"请再次输入密码" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    if (![password1 isEqualToString:password2]) {
        WISAlertView *alertView =
        [[WISAlertView alloc]initWithTitle:@"提示" message:@"两次输入密码不一样" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    //提交数据
    //注册请求参数
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //1.学生phone
    [dict setObject:self.phone forKey:@"phone"];
    
    //2.未加密密码
    [dict setObject:password2 forKey:@"passwords"];
    [dict setObject:password2 forKey:@"password"];
    
    
    //获取系统时间戳
    NSDate* date1 = [NSDate date];
    NSTimeInterval time1 =[date1 timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f",time1];
    DLog(@"系统时间戳:%@",timeString);
    
    //3.签名时间 当前时间
    [dict setObject:timeString forKey:@"signtimes"];
    //4.唯一标识符
    NSString *uuid = [NSUUID UUID].UUIDString;
    [dict setObject:uuid forKey:@"signuid"];
    
    //5.签名
    NSString *signflag = [SINAMd5Utils md5:[NSString stringWithFormat:@"%@%@",uuid,timeString]];
    [dict setObject:signflag forKey:@"signflag"];
    
    if (self.otherLoginTag ==1) {
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSString * openId =[userDefaults objectForKey:@"qqopenid"];
        NSString * nickname = [userDefaults objectForKey:@"qqnickname"];
        [dict setObject:openId forKey:@"qqopenid"];
        [dict setObject:nickname forKey:@"qqnickname"];
    }
    else if (self.otherLoginTag ==2)
    {
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSString * openId =[userDefaults objectForKey:@"weixinopenid"];
        NSString * nickname = [userDefaults objectForKey:@"weixinnickname"];
        [dict setObject:openId forKey:@"weixinopenid"];
        [dict setObject:nickname forKey:@"weixinnickname"];
    }
    
    NSMutableDictionary *dict2 = [[NSMutableDictionary alloc]init];
    
    [dict2 setObject:[SINAHttpRequestUtils encodeDicParams:dict] forKey:@"sj"];

    [self startLoading];
    
    if (self.pageType==0) {
        //self.navigationItem.title =@"注册";
        [self registerDataFunction:dict2];
    }else if(self.pageType==1){
        //self.navigationItem.title =@"找回密码";
        [self alertPasswordDataFunction:dict2];
    }
}
//注册数据 提交
-(void) registerDataFunction:(NSMutableDictionary*)dict2{
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userRegisternUrl] withParamters:dict2 success:^(id responseData) {
        
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
    
        if ([code isEqualToString:@"1000"]) {
            NSDictionary *result = responseData[@"content"];
            
            
            SINAUserModel *userModel=[SINAUserModel userWithDict:result];
            SINACommonUserHelper *userHelper=[SINACommonUserHelper share];
            [userHelper saveUserModel:userModel];
            
            
            if (userModel.userUid==nil||[userModel.userUid isEqualToString:@""]) {
                //登录页面
                SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
                SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
                [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];
                
            } else {
                SINAIdentitySelectViewController *selectControll = [[SINAIdentitySelectViewController alloc]initWithNibName:@"SINAIdentitySelectViewController" bundle:nil];
                [self.navigationController pushViewController:selectControll animated:YES];
            }
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"登录验证失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}
//忘记密码数据 提交
-(void) alertPasswordDataFunction:(NSMutableDictionary*)dic{
    WeakSelf;
    [CXAFNHttpRequestUtils httpPostRequest:[NSString stringWithFormat:@"%@%@",baseUrl,userForgetPasswordUrl] withParamters:dic success:^(id responseData) {
        NSString *code = responseData[@"code"];
         [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            [YPXToastView showToastViewWithText:@"修改成功，请重新登录" andDuration:2 andCorner:5 andParentView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                
            });
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =  [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
    } failure:^(NSError *error, NSInteger errCode) {
        [weakSelf stopLoading];
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"验证失败" sureBtn:@"确定" cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
     [YPXToastView showToastViewWithText:@"验证码验证成功 请设置密码" andDuration:2 andCorner:5 andParentView:self.view];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
